import Config from '@/common/config.js'

const API = {}
API.apiurl = Config.apiurl;
//API.imgurl = Config.imgurl;
API.isshop = Config.isshop;
API.apikey = Config.apikey;
API.getApiUrl = function() {
	return Config.apiurl;
}
API.checkSession = function(data) {
	return new Promise(function(resolve, reject) {
		uni.checkSession({
			success() {
				//session_key 未过期，并且在本生命周期一直有效
				var sessionid = uni.getStorageSync('sessionid');
				//var sessionidtime = uni.getStorageSync('sessionidtime');
				//var now = Math.floor(Date.now() / 1000);
				uni.login({
					success: (res) => {
						API.userLogin({
							parentid: data.parentid,
							code: res.code
						}).then((e) => {
							if (e.code == 1) {
								uni.setStorageSync('sessionid', e.data
									.sessionid)
								uni.setStorageSync('sessionidtime', e.data
									.time)
								resolve(e.data);
							}
						})
					},
				})
			},
			fail() {
				// session_key 已经失效，需要重新执行登录流程
				uni.login({
					success: (res) => {
						API.userLogin({
							parentid: data.parentid,
							code: res.code
						}).then((e) => {
							if (e.code == 1) {
								uni.setStorageSync('sessionid', e.data
									.sessionid)
								uni.setStorageSync('sessionidtime', e.data.time)
								resolve(e.data);
							}
						})
					},
				})
			}
		})

	})

}

API.showToast = function(title, url = "") {
	uni.showToast({
		icon: 'none',
		title: title,
		success() {
			if (url) {
				uni.reLaunch({
					url: url
				})
			}
		}
	})
}
API.formatDate = function(value, format = 'YmdHis', Separator = '-') {
	if (value == undefined) {
		return;
	}
	let date = new Date(value * 1000)

	let ret = '';
	if (format.indexOf('Y') > -1) {
		ret += date.getFullYear();
	}
	if (format.indexOf('m') > -1) {
		let MM = date.getMonth() + 1;
		MM = MM < 10 ? ('0' + MM) : MM;
		ret += ret ? Separator + MM : MM;
	}
	if (format.indexOf('d') > -1) {
		let d = date.getDate();
		d = d < 10 ? ('0' + d) : d;
		ret += ret ? Separator + d : d;
	}

	return ret;
}
API.showModal = function(content, url = '', to = "", confirmText = "知道了") {
	uni.showModal({
		title: '',
		content: content,
		showCancel: false,
		confirmText: confirmText,
		success: function(res) {
			if (res.confirm) {
				if (to == "navigateTo") {
					uni.navigateTo({
						url: url
					})
				} else
				if (to == "back") {
					uni.navigateBack({
						delta: 1
					})
				} else
				if (url != "") {
					uni.reLaunch({
						url: url
					})
				}
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
API.request = function(url, method = "GET", data = {}) {
	return new Promise(function(resolve, reject) {
		url = Config.apiurl + url;
		/*if((JSON.stringify(data) == "{}")){
			data = {apikey: Config.apikey};
		}else{
			data = Object.assign(data, {apikey: Config.apikey});
		}*/
		var sessionid = uni.getStorageSync('sessionid')
		uni.request({
			url: url,
			data: data,
			method: method,
			dataType: 'json',
			header: {
				'token': Config.apikey,
				'sessionid': sessionid,
				'lang': 'zh_cn',
				'Cache-Control': 'no-cache',
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			success: function(res) {
				if (res.statusCode == 200) {
					if (res.data.errCode != 0 && res.data.message) {
						uni.showToast({
							title: res.data.message,
							icon: 'error',
							duration: 3000
						});
					}
					resolve(res.data);
				} else {
					uni.showToast({
						title: '请求数据出错',
						icon: 'loading',
						duration: 1000
					});
					reject(res.data);
				}

			},
			fail: function(err) {
				reject(err);
			}
		})
	});

}

API.get = function(url, data = {}) {
	return API.request(url, "GET", data);
}

API.post = function(url, data) {
	return API.request(url, "POST", data);
}

export default API